package com.zzx.bms.core.util;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class PropertiesFileUtil {

    /**
     * @param filePath      文件路径
     * @param propertyNames 属性名
     * @return Map<String,String> 属性键值对map
     * @throws 文件未找到 ，io异常
     * @Title: getValue
     * @Description: 读取配置文件的值
     */
    public static Map<String, String> getValue(String filePath,
                                               String[] propertyNames) {
        Properties prop = new Properties();
        InputStream in = null;
        Map<String, String> map = new HashMap<String, String>(
                propertyNames.length);
        try {
            in = new FileInputStream(filePath);
            prop.load(in);
            for (String key : propertyNames) {
                if (null != prop.getProperty(key)) {
                    map.put(key, prop.getProperty(key).trim());
                }
            }
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null)
                    in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return map;
    }


    /**
     * @param filePath      文件路径
     * @param propertyNames 属性名
     * @return Map<String,String> 属性键值对map
     * @throws 文件未找到 ，io异常
     * @Title: getValue
     * @Description: 读取配置文件的值
     */
    public static Map<String, String> getValue(InputStream filePath,
                                               String[] propertyNames) {
        Properties prop = new Properties();
        Map<String, String> map = new HashMap<String, String>(
                propertyNames.length);
        try {
            prop.load(filePath);
            for (String key : propertyNames) {
                if (null != prop.getProperty(key)) {
                    map.put(key, prop.getProperty(key).trim());
                }
            }
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (filePath != null)
                    filePath.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return map;
    }

    public static String getKeyValue(InputStream filePath, String key) {
        String val = null;
        Properties prop = new Properties();
        try {
            prop.load(filePath);
            if (null != prop.getProperty(key))
                val = prop.getProperty(key).trim();
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (filePath != null)
                    filePath.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return val;
    }


    public static String getKeyValue(String filePath, String key) {
        String val = null;
        Properties prop = new Properties();
        InputStream in = null;
        try {
            in = new FileInputStream(filePath);
            prop.load(in);
            if (null != prop.getProperty(key))
                val = prop.getProperty(key).trim();
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null)
                    in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return val;
    }

    /**
     * @param filePath 文件路径
     * @return Map<String,String> 所有 属性键值对map
     * @throws 文件未找到 ，io异常
     * @Title: readProperties
     * @Description: 读取properties的全部信息
     */
    @SuppressWarnings("rawtypes")
    public static Map<String, String> getAllProperties(String filePath) {
        Properties props = new Properties();
        Map<String, String> map = new HashMap<String, String>();
        InputStream in = null;
        try {
            in = new FileInputStream(filePath);
            props.load(in);
            Enumeration en = props.propertyNames();
            while (en.hasMoreElements()) {
                String key = (String) en.nextElement();
                String Property = props.getProperty(key);
                map.put(key, Property);
            }
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null)
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
        return map;
    }

    /**
     * 秦冲
     * 获取webinfo下文件的路径
     *
     * @param @return
     * @return String
     * @throws
     * @Title: getWEBINFOPath
     * @Description: TODO
     */
    public static InputStream getWEBINFOPath_() {
//		 String path = PropertiesFileUtil.class.getProtectionDomain().getCodeSource().getLocation().getFile();

        ClassLoader classloader = Thread.currentThread().getContextClassLoader();
        InputStream is = classloader.getResourceAsStream("com/cableex/config/dataSource.properties");

//		 String url = PropertiesFileUtil.class.getResource("").getPath().replaceAll("%20", " ");  
//         String configpath = url.substring(0, url.indexOf("WEB-INF")) + "WEB-INF/config/"; 

		/*String configpath = Thread.currentThread().getContextClassLoader()
				.getResource("/").getPath();
		if (StringUtil.notEmpty(configpath)) {
			configpath = configpath.replace("classes/", "config/");
			configpath = configpath.substring(1, configpath.length());
		} else {
			configpath = "";
		}*/
        return is;
    }

    public static InputStream getSolarPath() {
        ClassLoader classloader = Thread.currentThread().getContextClassLoader();
        InputStream is = classloader.getResourceAsStream("com/cableex/config/solrconfig.properties");

        return is;
    }


    /**
     * 秦冲
     * 获取webinfo下文件的路径
     *
     * @param @return
     * @return String
     * @throws
     * @Title: getWEBINFOPath
     * @Description: TODO
     */
    public static String getWEBINFOCONFIGPath() {
//		 String path = PropertiesFileUtil.class.getProtectionDomain().getCodeSource().getLocation().getFile();


        String url = PropertiesFileUtil.class.getResource("").getPath().replaceAll("%20", " ");
        String configpath = url.substring(0, url.indexOf("WEB-INF")) + "WEB-INF/config/";
		
		/*String configpath = Thread.currentThread().getContextClassLoader()
				.getResource("/").getPath();
		if (StringUtil.notEmpty(configpath)) {
			configpath = configpath.replace("classes/", "config/");
			configpath = configpath.substring(1, configpath.length());
		} else {
			configpath = "";
		}*/
        return configpath;
    }


    /**
     * kevin_shen
     * 获取当前项目的路径
     */
    public static String getGaClassPath() {

        return PropertiesFileUtil.class.getResource("").getPath();
    }

    /**
     * 秦冲
     * 获取webinfo下文件的路径
     *
     * @param @return
     * @return String
     * @throws
     * @Title: getWEBINFOPath
     * @Description: TODO
     */
    public static String getWEBINFOClassesPath() {
        String url = PropertiesFileUtil.class.getResource("").getPath().replaceAll("%20", " ");
        String configpath = url.substring(0, url.indexOf("WEB-INF")) + "WEB-INF/classes/";
        return configpath;
    }

    /**
     * @param filePath       文件路径
     * @param parameterName  文件属性名
     * @param parameterValue 文件属性值
     * @throws
     * @Title: writeProperties
     * @Description: 写入properties信息
     */
    public static void writeProperties(String filePath, Map<String, String> map) {
        Properties prop = new Properties();
        InputStream fis = null;
        OutputStream fos = null;
        try {
            fis = new BufferedInputStream(PropertiesFileUtil.class
                    .getClassLoader().getResourceAsStream(filePath));
            prop.load(fis);
            fos = new FileOutputStream(filePath);
            for (String key : map.keySet()) {
                prop.setProperty(key, map.get(key));
                prop.store(fos, "Update '" + key + "' value");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != fos)
                    fos.close();
                if (null != fis)
                    fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        System.out.println("url-->" + PropertiesFileUtil.getValue(PropertiesFileUtil.getSolarPath(), new String[]{"mmb.solr.product.browse.url"}));

        ///E:/mmb2.0_workspaces/ga/bin/com/cableex/core/util/
        // Map<String, String> map = new HashMap<String, String>();
        // map.put("hello", "222");
        // PropertiesFileUtil.writeProperties("src/struts.properties", map);
        // String[]{"hello"}));
    }
}
